﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleJson;
using System.Security.Cryptography;
using SimpleJson;
namespace com.mogu.util
{
   public static class Tools
    {
        public static object getValue(this JsonObject obj,string key,object default_value)
        {
            object value = null;
            try
            {
                value = obj[key];
            }
            catch
            { 
            }
            if (value == null)
            {
                return default_value;
            }
            else
            {
                return value;
            }
             
        }

        public static object getValue(this JsonObject obj, string key)
        {
            object default_value = null;
            obj.TryGetValue(key, out default_value);
            return default_value;
        }

        public static string optString(this JsonObject obj, string key)
        {
            object default_value = null;
            obj.TryGetValue(key, out default_value);
            return (string)default_value;
        }
        public static int optInt(this JsonObject obj, string key)
        {
            object default_value = 0;
            obj.TryGetValue(key, out default_value);
            return (int)(long)default_value;
        }

        public static bool optBool(this JsonObject obj, string key)
        {
            object default_value = false;
            obj.TryGetValue(key, out default_value);
            return (bool)default_value;
        }

        public static void put(this JsonObject obj, string key,object value)
        {
            obj[key] = value;
        }
       

        public static long GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
        }
        public static long GetTimeMillis()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds);
        }
        public static string strMD5(String str)
        {
            byte[] result = Encoding.Default.GetBytes(str);    //tbPass为输入密码的文本框  
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToLower();
        }
        public static void sign(JsonObject parms, String token, String[] keys)
        {


            parms["timeline"] = GetTimeStamp();

            List<String> keys_list = new List<String>();
            for (int i = 0; i < keys.Length; i++)
            {
                keys_list.Add(keys[i]);
            }


            keys_list.Add("timeline");
            keys_list.Add("token");
            keys_list.Sort();

            StringBuilder buffer = new StringBuilder();
            int length = keys_list.Count;
            for (int i = 0; i < length; i++)
            {
                String key = keys_list[i];

                String value = null;
                if (key == "token")
                {
                    value = token;
                }
                else
                {
                    value = parms[key].ToString();
                }
                buffer.Append(key);
                buffer.Append("=");
                buffer.Append(value);
                if (i != length - 1)
                {
                    buffer.Append("&");
                }
            }
            String str = buffer.ToString();
            parms["sign"] = strMD5(str);

        }
    }
}
